package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class test3 {
    @Autowired
    private UserMapper userMapper;

    @Test
    //查询
    //id >= 1
    //name 模糊 j
    //password = 123456
    //age [0-30]
    //tel 12345678910,12345678911
    public void test3(){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(User::getAge,1)//id>1

                .like(User::getName,"j")

                .between(User::getAge,0,20)
                .eq(User::getPassword,"123456")
                .eq(User::getTel,"12345678910");
        List<User> userList = userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    //查询投影 排序
    @Test
    public void test1(){
        LambdaQueryWrapper<User> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.select(User::getAge,User::getName)//设置投影: select name,age from 表名.
                .orderByDesc(User::getAge);//设置排序: order by age asc/desc
        List<User> userList = userMapper.selectList(Wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    //分组 过滤
    //select name,count(*) as count from user group by name having count > 1
    @Test
    public void test2(){
        //分组和过滤只能使用QueryWrapper,
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("name", "count(*) as count");
        wrapper.groupBy("name");
        wrapper.having("count>1");
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        System.out.println(maps);
    }

    //分组 过滤
    //select name,count(*) as count from user group by name having count > 1
    @Test
    public void test4() {
        System.out.println(userMapper.count());
    }

    //分页
    @Test
    public void test5(){
        //2.设置参数
        Page<User> page = new Page<>(2, 2);//pagenum，pagesize
        //设置查询条件
        LambdaQueryWrapper<User> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.gt(User::getAge,1);//age>1
        //zhixingchaxun
        page= userMapper.selectPage(page, Wrapper);

        System.out.println("总条数"+page.getTotal());
        System.out.println("总页数"+page.getPages());
        System.out.println("当前页数据"+page.getRecords());
        System.out.println("当前页码值"+page.getCurrent());
        System.out.println("每页显示数"+page.getSize());
    }

//    @Test
//    public void testdelete(){
//        userMapper.deleteById(1L);
//    }
}
